package cn.usbtg.jfinal.plugin.autoroute;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * 手动路由对象
 */
public class ManualRoute {
    private Pattern pattern;//reg
    private String url;//reg url
    private HttpRequestMethod method;
    private String nativeUrl;//原生url
    private Map<String, String> params = new HashMap<>();//参数
    private boolean cache;//是否缓存

    public Pattern getPattern() {
        return pattern;
    }

    public void setPattern(Pattern pattern) {
        this.pattern = pattern;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public HttpRequestMethod getMethod() {
        return method;
    }

    public void setMethod(HttpRequestMethod method) {
        this.method = method;
    }

    public String getNativeUrl() {
        return nativeUrl;
    }

    public void setNativeUrl(String nativeUrl) {
        this.nativeUrl = nativeUrl;
    }

    public Map<String, String> getParams() {
        return params;
    }

    public void setParams(Map<String, String> params) {
        this.params = params;
    }

    public boolean isCache() {
        return cache;
    }

    public void setCache(boolean cache) {
        this.cache = cache;
    }

    public String getRoute() {
        return url.split(":")[1] + ":" + method.toString() + (cache ? ":CACHE" : ":NOCACHE") + "=>" + nativeUrl;
    }

    public String getInfo() {
        StringBuilder paramSb = new StringBuilder("{");
        StringBuilder paramSubSb = new StringBuilder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            paramSubSb.append(",").append(entry.getKey().substring(5)).append("=").append(entry.getValue());
        }
        paramSb.append(paramSubSb.length() > 0 ? paramSubSb.substring(1) : paramSubSb);
        paramSb.append("}");

        return url + "=>" + nativeUrl + ":" + paramSb.toString();
    }
}